<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="app">
        <button v-for="item in btnList" :key='item.title' @click='handleClick(item.title)'>{{item.name}}</button>
        <keep-alive :max="2">
            <component :is='comp'></component>
        </keep-alive>
    </div>
    <template id='news'>
        <div>
            <input type="text" v-model='val1' />
        </div>
    </template>
    <template id='happy'>
        <div>
            <input type="text" v-model='val2' />
        </div>
    </template>
    <template id='sport'>
        <div>
            <input type="text" v-model='val3' />
        </div>
    </template>
    <script src="../vue.js"></script>
    <script>
        let news={
            template:'#news',
            data () {
                return {
                    val1:'',
                }
            },
            activated () {
                console.log('new-activated');
            },
            deactivated () {
                console.log('new-deactivated');
            }
        }
        let happy={
            template:'#happy',
            data () {
                return {
                    val2:'',
                }
            }
        }
        let sport={
            template:'#sport',
            data () {
                return {
                    val3:'',
                }
            }
        }
        new Vue({
            el:'#app',
            components: {
                news,
                happy,
                sport,
            },
            data () {
                return {
                    comp:'news',
                    btnList:[
                        {title:'news',name:'新闻'},
                        {title:'happy',name:'娱乐'},
                        {title:'sport',name:'体育'},
                    ]
                }
            },
            methods: {
                handleClick(type){
                    if(type=='news'){
                        this.comp=type;
                    }else if(type=='happy'){
                        this.comp=type;
                    }else if(type=='sport'){
                        this.comp=type;
                    }
                }
            }
        })
    </script>
</body>
</html>